//
//  CZWelcomeViewController.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/2.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import SDWebImage

class CZWelcomeViewController: UIViewController {

    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        setupUI()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    
    //视图出现以后在调用动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    //设置动画视图出现完成以后开始调用动画
    private func startAnimation(){
    
        //更新约束
        
        self.photoImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 150)
            
            make.centerX.equalTo(self.view.snp_centerX)
            
            //设置一个宽高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        /*
        usingSpringWithDamping,弹簧的系数 0~1.0
        initialSpringVelocity,弹簧的初始速度
        枚举: 如果采用默认值,直接可以使用 []
        */

        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            //完成约束的时候我们需要调用 -- 面试的时候经常被问到
         
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                print("完成之后")
                
                //切换视图
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificaionChangeRootVC, object: nil)
                
        }

    }

    //添加控件约束
    private func setupUI(){
    
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
        
        //设置约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.centerX.equalTo(view.snp_centerX)
            //设置一个宽,高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
    
    nameLabel.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(photoImageView.snp_bottom).offset(10)
        make.centerX.equalTo(view.snp_centerX)
        
        }
        
        //设置头像的占位符//头像修改 -- placeholder

        let url = NSURL(string: CZUserViewModel.shareInstance.model?.profile_image_url ?? "")
    
        
        
        if let Ourl = url{
        
//            photoImageView.sd_setImageWithURL(Ourl , placeholderImage:UIImage(named: "avatar_default_big"))
        photoImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
    
    }
    
    
    
    
    //懒加载
    /**
    *  背景图
    */
    
    private lazy var bgImageView:UIImageView = {
    
    let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
    
    }()
    
    //设置头像
    private lazy var photoImageView: UIImageView = {
    
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 170/4
        //多余的裁剪掉
        img.layer.masksToBounds = true
        
        img.layer.borderColor = UIColor.orangeColor().CGColor
        
        img.layer.borderWidth = 2
        
    
        return img
    
    }()
    
    //设置用户名label
    private lazy var nameLabel:UILabel = {
    
        let label = UILabel()
        
        label.textAlignment = .Center
        label.font =  UIFont.systemFontOfSize(15)
        label.textColor = UIColor.orangeColor()
        
        //添加用户名
        label.text = "\(CZUserViewModel.shareInstance.model?.screen_name ?? "")欢迎归来"
        
        
        return label
    }()
    
    
    
}
